package com.example.android.story.Utils;


import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Ignore;
import android.arch.persistence.room.Index;
import android.arch.persistence.room.PrimaryKey;
import android.support.annotation.NonNull;

import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Random;

import cn.bmob.v3.BmobObject;

/**
 * Created by android on 18-5-19.
 */

@Entity(
        tableName = "_user"
)
public class User  {

    @NonNull
    @PrimaryKey
    @ColumnInfo(name="username")
    String name;

    @NonNull
    String password;

    @NonNull
    int coins;  //硬币,积分

    @NonNull
    int salt;


    public User(){
        coins=50;
        Random rand=new Random();
        salt=rand.nextInt(0x7fffffff);
    }

    @Ignore
    public User(String name,String password,int coins){
        this.name=name;
        this.coins=coins;
        this.salt=new Random().nextInt(0x7fffffff);
        String rhs=toSha1(addSalt(password,salt));
        this.password = rhs;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean comparePassword(String password){
        String rhs=toSha1(addSalt(password,salt));
        return rhs.equals(this.password);
    }

    public int getCoins() {
        return coins;
    }

    public void setCoins(int coins) {
        this.coins = coins;
    }

    public int getSalt() {
        return salt;
    }

    public void setSalt(int salt){this.salt=salt;}

    public String addSalt(String str,int salt){
        return str+Integer.toString(salt);
    }
    public String toSha1(String str){
        String rt="";
        try {
            MessageDigest md= MessageDigest.getInstance("SHA-1");
            md.update(str.getBytes("utf-8"));
            StringBuffer buffer = new StringBuffer();
            byte [] digest=md.digest();
            for(byte bytes: digest){
                buffer.append(String.format("%02x",bytes));
            }
            rt=buffer.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return rt;
    }
    public BmobObject getBmobObject(){
        UserObj obj=new UserObj();
        obj.setCoins(this.getCoins());
        obj.setName(this.getName());
        obj.setPassword(this.getPassword());
        obj.setSalt(this.getSalt());
        return obj;
    }
    public class UserObj extends cn.bmob.v3.BmobObject{
        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        private String name;

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        private String password;

        public Integer getCoins() {
            return coins;
        }

        public void setCoins(Integer coins) {
            this.coins = coins;
        }

        private Integer coins;

        public Integer getSalt() {
            return salt;
        }

        public void setSalt(Integer salt) {
            this.salt = salt;
        }

        private Integer salt;
    }
}
